package com.turing.post.common.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Jan 14, 2013
 */
@Stateless(mappedName = "common.impl.SysOperLogDaoImpl")
@Remote
@Local
public class SysOperLogDaoImpl implements SysOperLogDao {

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void insertSysOperLog(SysOperLog sysOperLog)
			throws PCPSApplicationException {
		sysOperLog.setLogId(commonDaoImpl.getUniqueIntegerKey());
		sysOperLog.setLogTime(commonDaoImpl.getDatabaseSysTime());
		GeneralDao.insert(_em, sysOperLog);
	}

	@Override
	public SysOperLog getSysOperLogById(BigInteger logId) {
		return _em.find(SysOperLog.class, logId);
	}

	@Override
	public void insertSysOperLogInfo(PCPSWebInput webInput, String operType,
			String logName, String remark) throws PCPSApplicationException {
		// 插入数据库日志
		if (webInput != null && webInput.getOperId() != null) {
			SysOperLog sysOperLog = new SysOperLog();
			sysOperLog.setOperId(webInput.getOperId());
			sysOperLog.setOperType(operType);
			sysOperLog.setLogName(logName);
			sysOperLog.setRemark(remark);
			if (webInput != null) {
				sysOperLog.setIpAddress(webInput.getIpAddress());
			}
			if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
				sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
						.substring(0, 6));
				sysOperLog.setBranchNo(webInput.getOperBranchNo());
			}
			sysOperLog.setLogId(commonDaoImpl.getUniqueIntegerKey());
			sysOperLog.setLogTime(commonDaoImpl.getDatabaseSysTime());
			GeneralDao.insert(_em, sysOperLog);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SysOperLog> getSysOperLogListByCondition(PCPSWebInput webInput,
			String operType, String startTime, String endTime, Integer page) {

		String branchNo = "";
		String sql = "";
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			branchNo = PCPSUtil.getIssueCompanyNo(webInput);
		}
		if (!PCPSUtil.isEmpty(branchNo)) {
			sql = "select * from SysOperLog t where issuecompanyno='"
					+ branchNo + "'";
		} else {
			sql = "select * from SysOperLog t where 1=1";
		}
		if (!PCPSUtil.isEmpty(operType)) {
			sql += " and operType='" + operType + "'";
		}
		if (!PCPSUtil.isEmpty(startTime)) {
			sql += " and LogTime>=to_date('" + startTime
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}

		if (!PCPSUtil.isEmpty(endTime)) {
			sql += " and LogTime<=to_date('" + endTime
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		sql += " order by LogTime desc";
		if (page != null && page == -1) {
			// 生成报表
			Query query = _em.createNativeQuery(sql, SysOperLog.class);
			return query.getResultList();
		} else {
			// 查询列表
			int maxResult = Config.PAGE_SIZE;
			int startPosition = maxResult * page;
			Query query = _em.createNativeQuery(sql, SysOperLog.class)
					.setFirstResult(startPosition).setMaxResults(maxResult);
			return query.getResultList();
		}
	}

	@Override
	public BigInteger getSysOperLogTotalByCondition(PCPSWebInput webInput,
			String operType, String startTime, String endTime) {
		String branchNo = "";
		String sql = "";
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			branchNo = PCPSUtil.getIssueCompanyNo(webInput);
		}
		if (!PCPSUtil.isEmpty(branchNo)) {
			sql = "select nvl(count(*),0) from SysOperLog t where issuecompanyno='"
					+ branchNo + "'";
		} else {
			sql = "select nvl(count(*),0) from SysOperLog t where 1=1";
		}
		if (!PCPSUtil.isEmpty(operType)) {
			sql += " and operType='" + operType + "'";
		}
		if (!PCPSUtil.isEmpty(startTime)) {
			sql += " and LogTime>=to_date('" + startTime
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		if (!PCPSUtil.isEmpty(endTime)) {
			sql += " and LogTime<=to_date('" + endTime
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void deleteSysOperLogByOperId(PCPSWebInput webInput,
			BigInteger operId) {
		String sql = "delete from sysoperlog where operid=" + operId;
		Query query = _em.createNativeQuery(sql);
		query.executeUpdate();
	}
	@Override
	public void deleteSysOperLogByLogId(PCPSWebInput webInput,
			BigInteger logId){
		String sql = "delete from sysoperlog where logid=" + logId;
		Query query = _em.createNativeQuery(sql);
		query.executeUpdate();
	}
}